<?php

// ------------------------------------------------------------------------

/**
 * Tests for file writability
 *
 * is_writable() returns TRUE on Windows servers when you really can't write to
 * the file, based on the read-only attribute.  is_writable() is also unreliable
 * on Unix servers if safe_mode is on.
 *
 * @access	private
 * @return	void
 */
if ( ! function_exists('is_really_writable'))
{
	function is_really_writable($file)
	{
		// If we're on a Unix server with safe_mode off we call is_writable
		if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
		{
			return is_writable($file);
		}

		// For windows servers and safe_mode "on" installations we'll actually
		// write a file then read it.  Bah...
		if (is_dir($file))
		{
			$file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));

			if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
			{
				return FALSE;
			}

			fclose($fp);
			@chmod($file, DIR_WRITE_MODE);
			@unlink($file);
			return TRUE;
		}
		elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
		{
			return FALSE;
		}

		fclose($fp);
		return TRUE;
	}
}

// ------------------------------------------------------------------------

/**
 * Class registry
 *
 * This function acts as a singleton.  If the requested class does not
 * exist it is instantiated and set to a static variable.  If it has
 * previously been instantiated the variable is returned.
 *
 * @access	public
 * @param	string	the class name being requested
 * @param	string	the directory where the class should be found
 * @param	string	the class name prefix
 * @return	object
 */
if ( ! function_exists('load_class'))
{
	function &load_class($class, $directory = 'library')
	{
		static $_classes = array();

		// Does the class exist?  If so, we're done...
		if (isset($_classes[$class]))
		{
			return $_classes[$class];
		}

		$name = FALSE;

		// Look for the class first in the local application/libraries folder
		// then in the native system/libraries folder
		if (file_exists(BASEPATH.$directory.'/'.$class.'.php'))
		{
			$name = $class;
		
			if (class_exists($name) === FALSE)
			{
				require(BASEPATH.$directory.'/'.$class.'.php');
			}
		}
		
		// Did we find the class?
		if ($name === FALSE)
		{
			// Note: We use exit() rather then show_error() in order to avoid a
			// self-referencing loop with the Excptions class
			exit('Unable to locate the specified class: '.$class.'.php');
		}

		// Keep track of what we just loaded
		is_loaded($class);

		$_classes[$class] = new $name();
		return $_classes[$class];
	}
}

// --------------------------------------------------------------------

/**
 * Keeps track of which libraries have been loaded.  This function is
 * called by the load_class() function above
 *
 * @access	public
 * @return	array
 */
if ( ! function_exists('is_loaded'))
{
	function is_loaded($class = '')
	{
		static $_is_loaded = array();

		if ($class != '')
		{
			$_is_loaded[strtolower($class)] = $class;
		}

		return $_is_loaded;
	}
}

/**
 * Write Log File
 *
 * Generally this function will be called using the global log_message() function
 *
 * @param	string	the error level
 * @param	string	the error message
 * @param	bool	whether the error is a native PHP error
 * @return	bool
 */

if ( ! function_exists('log_message'))
{
	function log_message($level = 'error', $message, $php_error = FALSE)
	{
		static $_log;

		if (log_threshold == 0)
		{
			return;
		}

		$_log =& load_class('Log');
		$_log->write_log($level, $message, $php_error);
	}
}


if ( ! function_exists('get_protocal'))
{
	function get_protocal($param)
	{
		return ( $param & 0xFFFF);
	}
}

if ( ! function_exists('get_login_type'))
{
	function get_login_type($param)
	{
		return ( $param >> 16 );
	}
}

if ( ! function_exists('db_set_charset'))
{
	function db_set_charset($charset, $collation, $conn)
	{
		// mysql_set_charset() requires PHP >= 5.2.3 and MySQL >= 5.0.7, use SET NAMES as fallback
		$use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE;
	
		if ($use_set_names === TRUE)
		{
			return @mysql_query("SET NAMES '$charset' COLLATE '$collation'", $conn);
		}
		else
		{
			return @mysql_set_charset($charset, $conn);
		}
	}
}
function escape_str($conn, $str, $like = FALSE)
{
	if (is_array($str))
	{
		foreach ($str as $key => $val)
		{
			$str[$key] = $this->escape_str($val, $like);
		}

		return $str;
	}

	if (function_exists('mysql_real_escape_string') AND is_resource($conn))
	{
		$str = mysql_real_escape_string($str, $conn);
	}
	elseif (function_exists('mysql_escape_string'))
	{
		$str = mysql_escape_string($str);
	}
	else
	{
		$str = addslashes($str);
	}

	// escape LIKE condition wildcards
	if ($like === TRUE)
	{
		$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
	}

	return $str;
}

function escape($conn, $str)
{
	if (is_string($str))
	{
		$str = "'".escape_str($conn, $str)."'";
	}
	elseif (is_bool($str))
	{
		$str = ($str === FALSE) ? 0 : 1;
	}
	elseif (is_null($str))
	{
		$str = 'NULL';
	}

	return $str;
}


